package com.tanhua.server.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.common.pojo.PublishInfo;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.pojo.FailurePublish;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PublishInfoServicce {
    @Autowired
    private QuanZiVerifyService quanZiVerifyService;

    @Autowired
    private UserInfoService userInfoService;

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    /**
     * 消息翻页
     *
     * @param page
     * @param pagesize
     * @param sortProp
     * @param sortOrder
     * @param id
     * @param sd
     * @param ed
     * @param uid
     * @param state
     * @return
     */
    public PageResultVo queryAll(Integer page, Integer pagesize, String sortProp, String sortOrder, String id, Long sd, Long ed, String uid, String state) {
        QueryWrapper<PublishInfo> queryWrapper1 = new QueryWrapper<>();
        QueryWrapper<PublishInfo> queryWrapper2 = new QueryWrapper<>();
        QueryWrapper<PublishInfo> queryWrapper3 = new QueryWrapper<>();
        QueryWrapper<PublishInfo> queryWrapper4 = new QueryWrapper<>();
        IPage<PublishInfo> iPage1 = new Page<>();
        IPage<PublishInfo> iPage2 = new Page<>();
        IPage<PublishInfo> iPage3 = new Page<>();
        IPage<PublishInfo> iPage4 = new Page<>();
        if (ObjectUtil.isNotEmpty(page)) {
            iPage1.setCurrent(page);
            iPage2.setCurrent(page);
            iPage3.setCurrent(page);
            iPage4.setCurrent(page);
        }
        if (ObjectUtil.isNotEmpty(pagesize)) {
            iPage1.setSize(pagesize);
            iPage2.setSize(pagesize);
            iPage3.setSize(pagesize);
            iPage4.setSize(pagesize);
        }
        if (StrUtil.equals(sortOrder, "ascending")) {
            queryWrapper1.orderByAsc("create_date");
            queryWrapper2.orderByAsc("create_date");
            queryWrapper3.orderByAsc("create_date");
            queryWrapper4.orderByAsc("create_date");
        }
        if (StrUtil.equals(sortOrder, "descending")) {
            queryWrapper1.orderByDesc("create_date");
            queryWrapper2.orderByDesc("create_date");
            queryWrapper3.orderByDesc("create_date");
            queryWrapper4.orderByDesc("create_date");
        }
        if (ObjectUtil.isNotEmpty(id)) {
            queryWrapper1.like("user_id", id);
            queryWrapper2.like("user_id", id);
            queryWrapper3.like("user_id", id);
            queryWrapper4.like("user_id", id);
        }
        if (ObjectUtil.isAllNotEmpty(sd, ed) && sd > 0 && ed > 0) {
            queryWrapper1.ge("create_date", sd).le("create_date", ed);
            queryWrapper2.ge("create_date", sd).le("create_date", ed);
            queryWrapper3.ge("create_date", sd).le("create_date", ed);
            queryWrapper4.ge("create_date", sd).le("create_date", ed);
        }
        if (ObjectUtil.isNotNull(uid)) {
            queryWrapper1.eq("user_id", uid);
            queryWrapper2.eq("user_id", uid);
            queryWrapper3.eq("user_id", uid);
            queryWrapper4.eq("user_id", uid);
        }
        //查询全部
        List<String> list1 = new ArrayList<>();
        list1.add("1");
        list1.add("2");
        list1.add("3");
        list1.add("4");
        list1.add("5");
        list1.add("6");
        queryWrapper1.in("state", list1);
        IPage<PublishInfo> infoIPage1 = this.quanZiVerifyService.queryPublishInfo(queryWrapper1, iPage1);

        //待审核
        List<String> list2 = new ArrayList<>();
        list2.add("1");
        list2.add("3");
        queryWrapper2.in("state", list2);
        IPage<PublishInfo> infoIPage2 = this.quanZiVerifyService.queryPublishInfo(queryWrapper2, iPage2);

        //已通过
        List<String> list3 = new ArrayList<>();
        list3.add("2");
        list3.add("5");
        queryWrapper3.in("state", list3);
        IPage<PublishInfo> infoIPage3 = this.quanZiVerifyService.queryPublishInfo(queryWrapper3, iPage3);


        //已驳回
        List<String> list4 = new ArrayList<>();
        list4.add("4");
        list4.add("6");
        queryWrapper4.in("state", list4);
        IPage<PublishInfo> infoIPage4 = this.quanZiVerifyService.queryPublishInfo(queryWrapper4, iPage4);

        List<PublishInfo> publishInfoList = new ArrayList<>();

        PageResultVo pageResultVo = new PageResultVo();
        pageResultVo.setPage(page);
        pageResultVo.setPagesize(pagesize);

        if (StrUtil.equals(state, "all") || state==null) {
            publishInfoList = infoIPage1.getRecords();
            pageResultVo.setCounts(Convert.toInt(infoIPage1.getTotal()));
            pageResultVo.setPages(Convert.toInt(infoIPage1.getPages()));
        } else if (StrUtil.equals(state, "3")) {
            publishInfoList = infoIPage2.getRecords();
            pageResultVo.setCounts(Convert.toInt(infoIPage2.getTotal()));
            pageResultVo.setPages(Convert.toInt(infoIPage2.getPages()));
        } else if (StrUtil.equals(state, "4")) {
            publishInfoList = infoIPage3.getRecords();
            pageResultVo.setCounts(Convert.toInt(infoIPage3.getTotal()));
            pageResultVo.setPages(Convert.toInt(infoIPage3.getPages()));
        } else if (StrUtil.equals(state, "5")) {
            publishInfoList = infoIPage4.getRecords();
            pageResultVo.setCounts(Convert.toInt(infoIPage4.getTotal()));
            pageResultVo.setPages(Convert.toInt(infoIPage4.getPages()));
        }

        List<Object> userIds = CollUtil.getFieldValues(publishInfoList, "userId");

        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoByUserIdList(userIds);

        List<PublishInfoVo> publishInfoVoList = new ArrayList<>();

        for (PublishInfo publishInfo : publishInfoList) {
            String publishId = publishInfo.getPublishId();
            String infoState = publishInfo.getState();
            Integer userId = publishInfo.getUserId();
            if (StrUtil.contains("25", infoState)) {
                Publish publish = this.quanZiApi.queryPublishById(publishId);
                for (UserInfo userInfo : userInfoList) {
                    if (ObjectUtil.equals(userId, Convert.toInt(userInfo.getUserId()))) {
                        PublishInfoVo publishInfoVo = this.fillPublishInfoVo(publishInfo, userInfo);
                        if (ObjectUtil.isNotNull(publish)) {
                            publishInfoVo.setText(publish.getText());
                            publishInfoVo.setMedias(publish.getMedias());
                        }else {
                            publishInfoVo.setText("假数据111111");
                            List<String> list = new ArrayList<>();
                            list.add("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/photo/6/CL-3.jpg");
                            list.add("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/photo/5/1564567248169.jpg");
                            list.add("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/photo/5/1564567248169.jpg");
                            publishInfoVo.setMedias(list);
                        }
                        publishInfoVoList.add(publishInfoVo);
                    }

                }
            } else {
                FailurePublish failurePublish = this.quanZiApi.queryFailurePublishById(publishId);
                for (UserInfo userInfo : userInfoList) {
                    if (ObjectUtil.equals(userId, Convert.toInt(userInfo.getUserId()))) {
                        PublishInfoVo publishInfoVo = this.fillPublishInfoVo(publishInfo, userInfo);
                        if (ObjectUtil.isNotNull(failurePublish)) {

                            publishInfoVo.setText(failurePublish.getText());
                            publishInfoVo.setMedias(failurePublish.getMedias());
                        } else {
                            publishInfoVo.setText("假数据111111");
                            List<String> list = new ArrayList<>();
                            list.add("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/photo/6/CL-3.jpg");
                            list.add("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/photo/5/1564567248169.jpg");
                            list.add("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/photo/5/1564567248169.jpg");
                            publishInfoVo.setMedias(list);
                        }

                        publishInfoVoList.add(publishInfoVo);
                    }

                }
            }
        }
        TitleVo titleVo1 = new TitleVo();
        titleVo1.setCode("all");
        titleVo1.setTitle("全部");
        titleVo1.setValue(Convert.toInt(infoIPage1.getTotal()));

        TitleVo titleVo2 = new TitleVo();
        titleVo2.setCode("3");
        titleVo2.setTitle("待审核");
        titleVo2.setValue(Convert.toInt(infoIPage2.getTotal()));

        TitleVo titleVo3 = new TitleVo();
        titleVo3.setCode("4");
        titleVo3.setTitle("已通过");
        titleVo3.setValue(Convert.toInt(infoIPage3.getTotal()));

        TitleVo titleVo4 = new TitleVo();
        titleVo4.setCode("5");
        titleVo4.setTitle("已驳回");
        titleVo4.setValue(Convert.toInt(infoIPage4.getTotal()));

        List<TitleVo> titleVoList = new ArrayList<>();
        titleVoList.add(titleVo1);
        titleVoList.add(titleVo2);
        titleVoList.add(titleVo3);
        titleVoList.add(titleVo4);

        pageResultVo.setItems(publishInfoVoList);
        pageResultVo.setTotals(titleVoList);
        return pageResultVo;
    }

    /**
     * 通过审核
     *
     * @param ids
     * @return
     */
    public String passPublishInfo(List<String> ids) {
        try {
            List<PublishInfo> publishInfoList = this.quanZiVerifyService.queryPublishInfoByIds(ids);
            List<String> publishIds = new ArrayList<>();
            if (ObjectUtil.isNotEmpty(publishInfoList)) {
                for (PublishInfo publishInfo : publishInfoList) {
                    publishIds.add(publishInfo.getPublishId());
                }
            }
            List<String> pass = this.quanZiVerifyService.pass(publishIds);
            for (String publishId : pass) {

                FailurePublish failurePublish = this.quanZiApi.queryFailurePublishById(publishId);
                Publish publish = new Publish();
                BeanUtil.copyProperties(failurePublish, publish);
                this.quanZiApi.savePublish(publish);
                this.quanZiApi.removeFailQuanzi(publishId);
            }
            return "成功";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "失败";
    }

    /**
     * 不通过
     *
     * @param ids
     * @return
     */
    public String rejectPublishInfo(List<String> ids) {
        try {
            List<PublishInfo> publishInfoList = this.quanZiVerifyService.queryPublishInfoByIds(ids);
            List<String> publishIds = new ArrayList<>();
            if (ObjectUtil.isNotEmpty(publishInfoList)) {
                for (PublishInfo publishInfo : publishInfoList) {
                    publishIds.add(publishInfo.getPublishId());
                }
            }
            this.quanZiVerifyService.reject(publishIds);
            return "成功";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "失败";
    }

    /**
     * 撤销
     *
     * @param ids
     * @return
     */
    public String revokePublishInfo(List<String> ids) {
        try {
            List<PublishInfo> publishInfoList = this.quanZiVerifyService.queryPublishInfoByIds(ids);
            List<String> publishIds = new ArrayList<>();
            if (ObjectUtil.isNotEmpty(publishInfoList)) {
                for (PublishInfo publishInfo : publishInfoList) {
                    publishIds.add(publishInfo.getPublishId());
                }
            }
            List<String> revoke = this.quanZiVerifyService.revoke(publishIds);
            for (String publishId : revoke) {
                Publish publish = this.quanZiApi.queryPublishById(publishId);
                FailurePublish failurePublish = new FailurePublish();
                this.quanZiApi.saveFailQuanzi(publish);
                this.quanZiApi.removeQuanzi(publishId);
            }
            return "成功";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "失败";


    }

    private PublishInfoVo fillPublishInfoVo(PublishInfo publishInfo, UserInfo userInfo) {
        PublishInfoVo publishInfoVo = new PublishInfoVo();
        publishInfoVo.setId(Convert.toStr(publishInfo.getId()));
        publishInfoVo.setNickname(userInfo.getNickName());
        publishInfoVo.setUserId(publishInfo.getUserId());
        publishInfoVo.setUserLogo(userInfo.getLogo());
        publishInfoVo.setCreateDate(Convert.toInt(publishInfo.getCreateDate()));
        publishInfoVo.setState(publishInfo.getState());
        publishInfoVo.setTopState(publishInfo.getTopState());
        publishInfoVo.setCommentCount(publishInfo.getCommentCount());
        publishInfoVo.setLikeCount(publishInfo.getLikeCount());
        publishInfoVo.setReportCount(publishInfo.getReportCount());
        publishInfoVo.setForwardingCount(publishInfo.getForwardingCount());
        return publishInfoVo;
    }

    public String top(String id) {
        PublishInfo publishInfo = this.quanZiVerifyService.queryPublishInfoById(id);
        Integer topState = publishInfo.getTopState();
        if (ObjectUtil.equal(topState, 2)) {
            return "失败";
        }
        this.quanZiVerifyService.top(id);
        return "成功";
    }

    public String untop(String id) {
        PublishInfo publishInfo = this.quanZiVerifyService.queryPublishInfoById(id);
        Integer topState = publishInfo.getTopState();
        if (ObjectUtil.equal(topState, 1)) {
            return "失败";
        }
        this.quanZiVerifyService.untop(id);
        return "成功";
    }

    public PublishInfoVo queryPublishInfo(String id) {
        PublishInfo publishInfo = this.quanZiVerifyService.queryPublishInfoById(id);
        String publishId = publishInfo.getPublishId();
        Integer userId = publishInfo.getUserId();

        UserInfo userInfo = this.userInfoService.queryUserInfoByUserId(Convert.toLong(userId));
        Publish publish = this.quanZiApi.queryPublishById(publishId);

        PublishInfoVo publishInfoVo = this.fillPublishInfoVo(publishInfo, userInfo);
        publishInfoVo.setText(publish.getText());
        publishInfoVo.setMedias(publish.getMedias());

        return publishInfoVo;
    }

    public PageResult queryComment(Integer pagesize, Integer page, String sortProp, String sortOrder, String messageID) {
        PublishInfo publishInfo = this.quanZiVerifyService.queryPublishInfoById(messageID);
        String publishId = publishInfo.getPublishId();
        PageInfo<Comment> pageInfo = this.quanZiApi.queryCommentList(publishId, page, pagesize);

        List<Comment> commentList = pageInfo.getRecords();
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        pageResult.setPages(Convert.toInt(Math.ceil(commentList.size() / Convert.toDouble(pagesize))));
        pageResult.setCounts(commentList.size());


        List<CommentUserVo> commentUserVoList = new ArrayList<>();
        for (Comment comment : commentList) {
            CommentUserVo commentUserVo = new CommentUserVo();
            commentUserVo.setId(Convert.toStr(comment.getId()));
            commentUserVo.setUserId(Convert.toInt(comment.getUserId()));
            commentUserVo.setContent(comment.getContent());
            commentUserVo.setCreateDate(Convert.toInt(comment.getCreated()));
            UserInfo userInfo = this.userInfoService.queryUserInfoByUserId(comment.getUserId());
            commentUserVo.setNickname(userInfo.getNickName());
            commentUserVoList.add(commentUserVo);
        }

        pageResult.setItems(commentUserVoList);

        return pageResult;
    }
}
